package com.template.mapper;


import com.template.entity.Post;
import com.template.entity.PostComment;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface CommunityMapper {

//    @Select("select * from post order by createTime DESC")
    @Select("select * from post order by createTime DESC")
    List<Post> getPostList();

    @Select("SELECT * FROM post WHERE title LIKE CONCAT('%', #{keyword}, '%') OR content LIKE CONCAT('%', #{keyword}, '%') OR author_name LIKE CONCAT('%', #{keyword}, '%')")
    List<Post> searchPosts(@Param("keyword") String keyword);

    @Insert("insert into post(authorId,author_name,author_avatar,imgUrl,title,content,likeManList,createTime,updateTime,isShow,postType) " +
            "values (#{authorId},#{authorName},#{authorAvatar},#{imgUrl},#{title},#{content},#{likeManList},#{createTime},#{updateTime},#{isShow},#{postType})")
    void addPost(Post post);

    @Select("select * from post where id=#{id}")
    Post getPostById(Integer id);

    @Update("update post set authorId=#{authorId},author_name=#{authorName},author_avatar=#{authorAvatar},imgUrl=#{imgUrl},title=#{title},content=#{content}" +
            ",likeManList=#{likeManList},createTime=#{createTime},updateTime=#{updateTime},isShow=#{isShow} where id=#{id}")
    void updatePostById(Post post1);

    @Insert("insert into postComment(post_id,comment_author_id,comment_name,comment,create_time,update_time,comment_avatar) values (#{postId},#{commentAuthorId},#{commentName}" +
            ",#{comment},#{createTime},#{updateTime},#{commentAvatar})")
    void addPostComment(PostComment postComment);

    @Select("select * from postComment where post_id=#{postId}")
    List<PostComment> getPostCommentList(Integer postId);
}
